// pages/error_book/index.js
import { IndexModel } from "../../models/index.js";
const indexModel = new IndexModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        noteList:null,
        courseId:0,
        loading: false,
        page: 1,
        total:0,
        noteMsg:'',
        noteDivShow:false,
        noteBtnColor:'#3e87f59d',//记笔记按钮颜色
        noteId:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
     onLoad(options) {
        wx.showLoading({
            title: '加载中',
          });
        //加载数据
      this.loadData(options.courseId,1);
      wx.hideLoading();

      
    },
    async loadData(courseId,page){
        const data= await indexModel.getNoteList(courseId,page);
        if(page!=1){
            const tempArray = this.data.noteList.concat(data.noteList);
            const newpage=(data.noteList.length>0?page:this.data.page);
            this.setData({
                noteList: tempArray,
                page:newpage,
                total: data.total,
                loading:false
              });
        }else{
            this.setData({
                courseId:courseId,
                noteList: data.noteList,
                total: data.total
              });
        }
    },

    //删除笔记
     delNote(event){
        let noteId=event.currentTarget.dataset.id;
        var that=this;
        wx.showModal({
            title: '',
            content: '确认要删除此笔记么？',
            confirmText:'删除',
            cancelText:'取消',
            async success (res) {
                if (res.confirm) {
                    await indexModel.delNote(noteId);
                    wx.showToast({
                        title: "删除成功",
                        icon: 'none',
                        duration: 2000
                      })
                    that.loadData(that.data.courseId,1);
                }
            }
        })
    },
    editNote(event){
        //记笔记页面展示
        let noteDivShow=true;
        let noteMsg=event.currentTarget.dataset.note;
        let noteId=event.currentTarget.dataset.id;
        if(event.currentTarget.dataset.type==1){
            noteDivShow=false;
            noteMsg='';
            noteId=0;
        }
        this.setData({
            noteDivShow:noteDivShow,
            noteMsg:noteMsg,
            noteId:noteId
        });
    },
    bindNoteInput(event){
        let noteBtnColor="#3e87f59d";
        if(event.detail.value!=''){
            noteBtnColor="#3e88f5";
        }
        this.setData({
            noteBtnColor:noteBtnColor,
            noteMsg:event.detail.value
        });
    },
    noteSubmit(event){
        //提交记的笔记
        let noteMsg=this.data.noteMsg;
        if(this.data.noteMsg!=''){
            indexModel.updateNote(this.data.noteId,noteMsg);
            wx.showToast({
                title: '修改成功',
                icon: 'none',
                duration: 2000
            });
            let noteData=this.data.noteList;
           for(let i=0;i< noteData.length;i++){
               if(noteData[i]['id']==this.data.noteId){
                noteData[i]['note_msg']=noteMsg;
               }
           }
           this.setData({
             noteList:noteData
           });
        }
        this.setData({
            noteDivShow:false
        });
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this._isLocked()) {
            return;
          }
          if (this._hasMore()) {
            this._locked();
            this.loadData(this.data.courseId,this.data.page + 1);
          }
    },
    _hasMore() {
        if (this.data.noteList.length >= this.data.total) {
          return false;
        } else {
          return true;
        }
      },
      _isLocked() {
        return this.data.loading ? true : false;
      },
      _locked() {
        this.setData({
          loading: true
        });
      },
      _unLocked() {
        this.setData({
          loading: false
        });
      },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})